<?php if(!defined('IN_APP')) exit('No direct script access allowed');

class mysql_database
{
    private $_escape_char = '`';
    private $_count_query = 'SELECT COUNT(*) AS ';
    private $_random_keyword = ' RAND()';
    
    private $_conn = false;
    private $_db = false;
    
    private $_fetchMethod = 'array'; // object, record
    
    // Query Variables
    private $_select = '*';
    private $_where;
    private $_order_by;
    private $_set;
    private $_group_by;
    private $_like;
    
    // Public functions
    
    public function connect($persistant=false)
    {
        if($persistant)
            $this->_conn = @mysql_pconnect(DB_HOST, DB_USER, DB_PASS);
        else
            $this->_conn = @mysql_connect(DB_HOST, DB_USER, DB_PASS, true);
        if(defined('DB_NAME'))
            $this->_db_select();
        return $this->_conn;
    }
    
    public function reconnect()
    {
        if(mysql_ping($this->_conn) === false)
            $this->_conn = false;
    }
    
    public function affected_rows()
    {
        return @mysql_affected_rows($this->_conn);
    }
    
    public function insert_id()
    {
        return @mysql_insert_id($this->_conn);
    }
    
    public function get($table='')
    {
        $query = $this->_build_query($table);
        $result = $this->_execute($query);
        switch($this->_fetchMethod)
        {
            case 'array' :
                return $result;
                break;
        }
    }
    
    // Private Functions
    
    private function _build_query($table)
    {
        $query = "SELECT {$this->_select} FROM {$table} ";
        $query .= $this->_parse_where($this->_where);
        $query = rtrim($query, " ");
        return $query;
    }
    
    private function _parse_fields( $fields )
    {
        $fields = $this->_escape_string($fields);
        return (is_array($fields)) ? implode(',', $fields) : $fields;
    }
    private function _parse_set($set)
    {
        if(is_array($set))
        {
            $return = '';
            foreach($set as $field => $value)
            {
                $value = $this->_escape_string($value);
                $return .= "{$field}={$value},";
            }
            $return = rtrim($return,',');
        }
        else
        {
            $return = $set;
        }
        return $return;
    }
    private function _parse_tables($tables)
    {
        $return = '';
        if(is_array($tables))
        {
            foreach($tables as $table)
            {
                switch($table['join'])
                {
                    case 'from' : $join = "{$table['table']} "; break;
                    case 'left' : $join = "LEFT JOIN {$table['table']} "; break;
                    case 'right': $join = "RIGHT JOIN {$table['table']} "; break;
                }
                if(isset($table['on']))
                {
                    $join .= "ON {$table['on']} ";
                }
                $return .= $join;
            }
        }
        return $return;
    }
    private function _parse_where( $where ) { return ($where != '') ? " WHERE $where" : ''; }
    
    private function _execute($query)
    {
        $query = $this->_prep_query($query);
        $this->_last_query = $query;
        $this->_query = @mysql_query($query, $this->_conn);
        return $this->_query;
    }
    
    private function _prep_query($query)
    {
        // Delete Hack will be added later
        return $query;
    }
    
    private function _error_message()
    {
        return @mysql_error($this->_conn);
    }
    private function _error_number()
    {
        return @mysql_errno($this->_conn);
    }
    
    private function _escape_string($str)
    {
        if(is_array($str))
        {
            foreach($str as $key => $value)
            {
                $str[$key] = $this->_escape_string($value);
            }
            return $str;
        }
        if(function_exists('mysql_real_escape_string') AND is_resource($this->_conn))
        {
            $str = mysql_real_escape_string($str, $this->_conn);
        }
        elseif(function_exists('mysql_escape_string'))
        {
            $str = mysql_escape_string($str);
        }
        else
        {
            $str = addslashes($str);
        }
        return $str;
    }
    
    private function _escape_indetifiers($idents)
    {
        // Continue Work On this...
        if($this->_escape_char == '')
        {
            return $idents;
        }
        return $idents;
    }
    
    private function _db_select()
    {
        $this->_db = @mysql_select_db(DB_NAME, $this->_conn);
        return $this->_db;
    }
}

/* EOF: mysql.database.php */